﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Ewts.Code
{
    public class NumGeneration
    {

        /// <summary>
        /// 根据左边的字符串及类型返回顺序编码号
        /// </summary>
        /// <param name="strLeft">左边字符串</param>
        /// <param name="serialType">LoadNum、CJO、WRL、BarCode、OutCode、InCode、VLoadNum</param>
        /// <returns>需要返回的序列号</returns>
        public static string GetSerialNum(string strLeft, string serialType)
        {
            string str = "";
            DataTable dt = new DataTable();
            int maxserial = 0;
            string fieldname = "";
            int numlen=0;
            switch (serialType)
            {
                case "LoadNum":
                    maxserial = new DataLib.BLL.tab_Num().GetNoByType(serialType);
                    fieldname = "loadnum";
                    numlen = 7;
                    //dt = new DataLib.BLL.LoadNum().GetList(1, "", "SUBSTRING(" + fieldname + ",3,9) desc").Tables[0];
                    break;
                case "CJO":
                    fieldname = "cjono";
                    numlen = 3;
                    maxserial = new DataLib.BLL.Cjo().MaxCjonoByLoad(strLeft);
                    break;
                case "WRL":
                    fieldname = "wrlno";
                    numlen = 7;
                    maxserial = new DataLib.BLL.tab_Num().GetNoByType(serialType);
                    //dt = new DataLib.BLL.InStorage().GetList(1txtplatenum, "Left(" + fieldname + ",1)='M'", "SUBSTRING(" + fieldname + ",2,8) desc").Tables[0];
                    break;
                case "PinNo":
                    fieldname = "PinNo";
                    numlen = 7;
                    maxserial = new DataLib.BLL.tab_Num().GetNoByType(serialType);
                    //dt = new DataLib.BLL.InStorage().GetList(1txtplatenum, "Left(" + fieldname + ",1)='M'", "SUBSTRING(" + fieldname + ",2,8) desc").Tables[0];
                    break;
                case "BarCode":
                    fieldname = "barcodevalue";
                    numlen = 3;
                    dt = new DataLib.BLL.BarCode().GetList(1, "Left(" + fieldname + ",27) = '" + strLeft + "'", "SUBSTRING(" + fieldname + ",28,3) desc").Tables[0];
                    break;
                case "OutCode":
                    fieldname = "outStorageNo";
                    numlen = 7;
                    switch (strLeft)
                    {
                        case "X":
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("XoutNo");
                            break;
                        case "C":
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("CoutNo");
                            break;
                        case "S":
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("SoutNo");
                            break;
                        case "F":
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("FoutNo");
                            break;
                        case "Z":
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("ZoutNo");
                            break;
                        case "T":
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("ToutNo");
                            break;
                        default:
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("YoutNo");
                            break;
                    }
                    //dt = new DataLib.BLL.OutStorage().GetList(1, "Left(" + fieldname + ",1)='" + strLeft + "'", "SUBSTRING(" + fieldname + ",2," +numlen+ ") desc").Tables[0];
                    break;
                case "InCode":
                    fieldname = "wrlno";
                    numlen = 6;
                    switch (strLeft)
                    {
                         case "RC":
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("CInNo");
                            break;
                        case "RS":
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("SInNo");
                            break;
                        case "RF":
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("FInNo");
                            break;
                        case "RZ":
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("ZInNo");
                            break;
                        case "RB"://领料
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("BInNo");
                            break;
                        default:
                            maxserial = new DataLib.BLL.tab_Num().GetNoByType("YInNo");
                            break;
                    }
              
                    //dt = new DataLib.BLL.InStorage().GetList(1, "Left(" + fieldname + ",2)='RF'", "SUBSTRING(" + fieldname + ",3,8) desc").Tables[0];
                    break;
                case "VLoadNum":
                    fieldname = "VLoadNum";
                    numlen = 8;
                    maxserial = new DataLib.BLL.tab_Num().GetNoByType("VLoadNum");
                    //dt = new DataLib.BLL.OutStorage().GetList(1, "Left(" + fieldname + ",2)='" + strLeft + "'", "SUBSTRING(" + fieldname + ",3,8) desc").Tables[0];
                    break;
                default:
                    break;
            }

            if (maxserial == 0)
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    string strtemp = dt.Rows[0][fieldname].ToString();
                    strtemp = strtemp.Remove(0, strLeft.Length);
                    maxserial = int.Parse(strtemp) + 1;
                }
                else
                {
                    maxserial = 1;
                }
            }
            str = strLeft+ maxserial.ToString().PadLeft(numlen, '0');
            return str;
        }


   

      
    }
}